/*
 *   Copyright (C) 2014  Raghavan Santhanam, raghavanil4m@gmail.com.
 *   I wrote this cross-platform talk tool, WindowsLinuxTalk, for the
 *   love of programming entirely under Ubuntu(Linux) for Windows in
 *   a remote fashion. Send any opinions/suggestions/bug reports to
 *   raghavanil4m@gmail.com.
 *
 *   WindowsExecHelper.c is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   WindowsExecHelper.c is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* WindowsExecHelper.c -- Loops over launching WindowsExec.exe if it any
 * terminates abruptly. */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include <windows.h>
#include <winbase.h>

int main(void)
{
 	while (1) {
        STARTUPINFO startupInfo = { 0 };
        PROCESS_INFORMATION procInfo = { 0 };
              
        bool created = CreateProcess("c:\\windows\\system32\\cmd.exe",
                                        "/C c:\\WindowsLinuxTalk\\WindowsExec.exe",
                                        NULL, NULL, TRUE,
                                        NORMAL_PRIORITY_CLASS, NULL, NULL,
                                        &startupInfo, &procInfo);
        if (created) {
             WaitForSingleObject(procInfo.hProcess, INFINITE);
             CloseHandle(procInfo.hThread);
             procInfo.hThread = INVALID_HANDLE_VALUE;
             CloseHandle(procInfo.hProcess);
             procInfo.hProcess = INVALID_HANDLE_VALUE;
        }
    }
    
    return 0;
}
